package com.zhiyou100.taxi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.taxi.dao.CustomerMapper;
import com.zhiyou100.taxi.entity.Car;
import com.zhiyou100.taxi.entity.Customer;
import com.zhiyou100.taxi.entity.CustomerExample;
import com.zhiyou100.taxi.service.CarOutService;
import com.zhiyou100.taxi.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-23 22:05
 * @Description
 */
@Transactional
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerMapper customerMapper;


    /**
     * 获取所有客户
     *
     * @return
     */
    @Override
    public List<Customer> getAll() {
        List<Customer> list = customerMapper.selectByExample(null);
        return list;
    }


    /**
     * 保存客户的信息
     *
     * @param customer
     * @return
     */
    @Override
    public Integer saveCustomer(Customer customer) {
        return customerMapper.insertSelective(customer);
    }

    /**
     * 根据身份证查询客户的信息
     *
     * @param pid
     * @return
     */
    @Override
    public Customer getCustomerByPid(String pid) {
       /* CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        criteria.andCustomerPidEqualTo(pid);
        List<Customer> list = customerMapper.selectByExample(example);
        System.out.println("list = " + list);
        if (list.size() > 0) {
            return list.get(0);
        }*/

        List<Customer> list = customerMapper.selectByCustomerPid(pid);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public String getPrimaryKey() {
        return "customer_" + customerMapper.selectPrimaryKey();
    }

    @Autowired
    CarOutService carOutService;

    //根据主键删除包括关联的表
    @Override
    public int deletePrimaryKey(String customerId) {
        carOutService.deleteByCustomerId(customerId);
        return customerMapper.deleteByPrimaryKey(customerId);
    }

    //根据客户主键获取 客户
    @Override
    public Customer getCustomerByPrimaryKey(String id) {
        Customer customer = customerMapper.selectByPrimaryKey(id);
        return customer;
    }

    @Override
    public PageInfo<Customer> getAllPage(Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Customer> list = getAll();
//        PageInfo<Customer> pagei = new PageInfo<Customer>(list);

        //分页插件使用 pageHelper工具类完成分页设置
//        Page<Object> page = PageHelper.startPage(pageNum, 5);
        PageInfo<Customer> pageInfo = new PageInfo<Customer>(list);
        return pageInfo;
    }

    @Override
    public int updateOne(Customer customer) {
        return customerMapper.updateByPrimaryKey(customer);

    }
}
